import 'package:flutter/material.dart';
import 'base_layout.dart';
import 'utils.dart';

///横枞向部局
class LinearLayout extends BaseLayout {
  //部局方向
  final Axis orientation;
  //子元素列表
  final List<Widget> children;
  //主轴对齐方式
  final MainAxisAlignment mainAxisAlignment;
  //主轴方向所占用的尺寸
  final MainAxisSize mainAxisSize;
  //交叉轴对齐方式
  final CrossAxisAlignment crossAxisAlignment;

  LinearLayout({
    Key key,
    double width,
    double height,
    bool isVisible,
    Decoration decoration,
    EdgeInsetsGeometry padding,
    EdgeInsetsGeometry margin,
    AlignmentGeometry alignment,
    this.orientation,
    this.children = const <Widget>[],
    this.mainAxisAlignment = MainAxisAlignment.start,
    this.mainAxisSize = MainAxisSize.min,
    this.crossAxisAlignment = CrossAxisAlignment.center,
  }) : super(
            key:key,
            alignment:alignment,
            width: width,
            height: height,
            isVisible: isVisible,
            padding: padding,
            margin: margin,
            decoration: decoration);

  @override
  _LinearLayoutState createState() => _LinearLayoutState();
}

class _LinearLayoutState extends State<LinearLayout> with BaseState {
  @override
  void initState() {
    baseLayout = widget;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
   return buildState(buildContent);
  }

  @override
  Widget buildContent() {
    if (widget.orientation == Axis.horizontal) {
      return _buildHorizontalWidget();
    } else {
      return _buildVerticalWidget();
    }
  }

  Widget _buildVerticalWidget() {
    return Column(
      mainAxisSize: widget.mainAxisSize,
      mainAxisAlignment: widget.mainAxisAlignment,
      crossAxisAlignment: widget.crossAxisAlignment,
      children: widget.children,
    );
  }

  Widget _buildHorizontalWidget() {
    return widget.mainAxisSize==null?Wrap(
      direction: widget.orientation,
      alignment: getWrapAlignment(widget.mainAxisAlignment),
      spacing: 0.0,
      runAlignment: getWrapAlignment(widget.crossAxisAlignment),
      runSpacing: 0.0,
      children: widget.children,
    ):Row(
      mainAxisSize: widget.mainAxisSize,
      mainAxisAlignment: widget.mainAxisAlignment,
      crossAxisAlignment: widget.crossAxisAlignment,
      children: widget.children,
    );
  }
}
